package org.mathig.poker.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapOfList<K, V> {

	private Map<K, List<V>> map = new HashMap<K, List<V>>();

	public void add(K key, V value) {
		List<V> list = get(key);
		list.add(value);
	}

	public List<V> get(K key) {
		List<V> list = map.get(key);
		if (list == null) {
			list = new ArrayList<V>();
			map.put(key, list);
		}
		return list;
	}

	public int count(K key) {
		return get(key).size();
	}

	public List<List<V>> valuesBySizeDesc() {
		List<List<V>> hash = new ArrayList<List<V>>();
		hash.addAll(map.values());
		Collections.sort(hash, new Comparator<List<?>>() {
			public int compare(List<?> l1, List<?> l2) {
				return new Integer(l2.size()).compareTo(l1.size());
			};
		});
		return hash;
	}

}
